package com.cloudsigns.factorymanager2.engineering.model;

import com.cloudsigns.factorymanager2.MyApplicaton;

import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tony on 2018/5/5.
 */

public class AddPartrolOrderModel implements InAddPartrolOrderModel {

    @Override
    public void addPartrolOrder(final long beginDate, final long completeDate, final List<MediaBean> mediaList, final String regionId, final String regionName, final OnAddPartrolOrderListener addPartrolOrderListener) {
        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.ADD_PARTROL_ORDER_URL);

                HttpResponse response;
                try {
                    // 添加http头信息
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("beginDate", beginDate);
                    jsonObject.put("completeDate", completeDate);

                    JSONArray mediaListJsonArr = new JSONArray();
                    if (null != mediaList) {
                        for (MediaBean mediaRegionBean : mediaList) {
                            JSONObject mediaJsonObj = new JSONObject();
                            mediaJsonObj.put("areaName", mediaRegionBean.getAreaName());
                            mediaJsonObj.put("mediaName", mediaRegionBean.getMediaName());
                            mediaJsonObj.put("number", mediaRegionBean.getNumber());
                            mediaJsonObj.put("positionX", mediaRegionBean.getPositionX());
                            mediaJsonObj.put("positionY", mediaRegionBean.getPositionY());
                            mediaJsonObj.put("regionId", mediaRegionBean.getRegionId());
                            mediaJsonObj.put("regionName", mediaRegionBean.getRegionName());

                            mediaListJsonArr.put(mediaJsonObj);
                        }
                    }
                    jsonObject.put("mediaList", mediaListJsonArr);

                    jsonObject.put("regionId", regionId);
                    jsonObject.put("regionName", regionName);


                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);

                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());

                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != addPartrolOrderListener) {

                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {

                            addPartrolOrderListener.addPartrolOrderSuccessed(json.getString("id"));
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            addPartrolOrderListener.addPartrolOrderFailed(error_str);


                        }

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnAddPartrolOrderListener {
        void addPartrolOrderSuccessed(String id);

        void addPartrolOrderFailed(String desc);
    }
}
